<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
use Home\Model\User;       //User模型
//测试控制器
class LoginController extends Controller{
	private $data;
	//构造函数
	public function __init(){
		$this->data = new User;
	}
	
    //载入模板
    public function index(){   	
       	if(IS_POST){
       		$username = Q('post.username');
			$password = Q('post.password');
			if($this->data->Vlogin($username,$password)){
				//如果有历史跳转则登陆后跳转回去
				if(isset($_SESSION['history'])){
					$url = $_SESSION['history'];
					Session::del('history');
					go($url);
				}
				go(U('Index/index'));
			}
			$this->error($this->data->getError());
       	}
        View::make();
    }
	//验证用户名
	public function check(){
		$name = Q('post.username');
		$username = $this->data->where('username','=',$name)->get();
		if($username){
			$this->ajax(array('status'=>1,'message'=>'用户名正确'));
		}else{
			$this->ajax(array('status'=>0,'message'=>'用户名不存在'));
		}
	}
	//生成验证码
	public function code(){
		Code::make();
	}
	//验证验证码
	public function codeCheck(){
		$code = Q('post.code');
		$code = strtolower($code);
		$session= Session::get('code');
		$session = strtolower($session);
		if($code != $session){
			$this->ajax(array('status'=>0,'message'=>'验证码不正确'));
		}else{
			$this->ajax(array('status'=>1,'message'=>'验证码正确'));
		}
		
	}
	//退出
	public function out(){
		session_unset();
		session_destroy();
		go(U('Index/index'));
	}
	
	
}
